package com.tbit.uqbike.webmanager.sms;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tbit.uqbike.webmanager.util.DateUtils;
import com.tbit.uqbike.webmanager.util.LogUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;

/**
 * @Author: chen
 * @Description: 国际化短信、亿美短信
 * @Date: 创建于 14:15 2021/3/12
 */
@Slf4j
public class InternationalSmsUtil {

    // appId
    public static String appId = "EUCP-EMY-IMS0-0XHOQ";// 请联系销售，或者在页面中 获取
    // 密钥
    public static String secretKey = "6E18C609513C2600";// 请联系销售，或者在页面中 获取
    // 接口地址
    public static String host = "http://sdkint.eucp.b2m.cn:80/inter/sendSingleIMS";// 请联系销售获取

    /**
     * @param appId
     * @param secretKey
     * @param host
     * @param phone     手机号码 格式为00+区号+号码
     * @param content   发送内容
     * @return
     */
    public static Boolean send(String appId, String secretKey, String host, String phone, String content) {

        String result = "";
        JSONObject jsonObject = new JSONObject();
        String times = DateUtils.parseDateToStr(DateUtils.YYYYMMDDHHMMSS, new Date());

        String sign = Md5.md5((appId + secretKey + times).getBytes());
        jsonObject.put("appId", appId);
        jsonObject.put("timestamp", times);
        jsonObject.put("sign", sign);
        jsonObject.put("mobile", phone);
        jsonObject.put("content", content);
        String json = jsonObject.toJSONString();
        System.out.println(jsonObject.toJSONString());
        byte[] bytes = null;
        try {
            bytes = json.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            log.error(e.getMessage(), e);
        }

        String url = host;
        URL realUrl = null;
        try {
            realUrl = new URL(url);
        } catch (MalformedURLException e) {
            log.error(e.getMessage(), e);
        }
        // 打开和URL之间的连接
        URLConnection conn = null;
        try {
            conn = realUrl.openConnection();

            // 设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            conn.setRequestProperty("Content-Type", "application/octet-stream");
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);

            // 发送请求参数
            DataOutputStream dos = null;

            dos = new DataOutputStream(conn.getOutputStream());
            dos.write(bytes);

            // flush输出流的缓冲
            dos.flush();
            // 定义BufferedReader输入流来读取URL的响应
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
            LogUtil.info("国际短信发送=" + result);
            //{"code":"SUCCESS","data":{"imsId":"161552957007500699","mobile":"0016462662535","customImsId":null}}
            if (result != null && !"".equals(result)) {
                JSONObject jsonObject1 = JSON.parseObject(result);
                if ("SUCCESS".equals(jsonObject1.getString("code"))) {
                    return true;
                }
            }
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            return false;
        }
        return false;
    }

    /**
     * 发送
     *
     * @param phone
     * @param content
     * @return
     */
    public static boolean send(String phone, String content) {
        return send(appId, secretKey, host, phone, content);
    }

    public static void main(String[] args) {
        System.out.println(send(appId, secretKey, host, "0066987298527", "Your verification code is:156324"));
    }
}
